home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / TubeTest / TubeTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  10.6 KB  |  370 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        TubeTest.c
  3.  
  4.     Contains:    The TubeTest program is a simple demonstration of how to use the Palette 
  5.                 Manager in a color program.  It has a special color palette that is associated
  6.                 with the main window.  The colors are animated using the Palette Manager 
  7.                 to give a flowing tube effect.  The program is very simple, and the Palette
  8.                 Manager and drawing parts are put in separate subroutines to make it easier
  9.                 to figure out what is happening.
  10.     
  11.                 The program is still a complete Macintosh application with a Main Event Loop,
  12.                 so there is the extra code to run the MEL.  
  13.     
  14.                 There is a resource file that is necessary as well, to define the Menus, Window,
  15.                 Dialog, and Palette resources used in the program.  
  16.     
  17.                 See Sample and TESample for the general structure and MultiFinder techniques that
  18.                 we recommend that you use when building a new application.
  19.  
  20.     Written by: BJ    
  21.  
  22.     Copyright:    Copyright © 1988-1999 by Apple Computer, Inc., All Rights Reserved.
  23.  
  24.                 You may incorporate this Apple sample source code into your program(s) without
  25.                 restriction. This Apple sample source code has been provided "AS IS" and the
  26.                 responsibility for its operation is yours. You are not permitted to redistribute
  27.                 this Apple sample source code as "Apple sample source code" after having made
  28.                 changes. If you're going to re-distribute the source, we require that you make
  29.                 it clear in the source that the code was descended from Apple sample source
  30.                 code, but that you've made changes.
  31.  
  32.     Change History (most recent first):
  33.                 08/2000        JM                Carbonized, non-Carbon code is commented out
  34.                                             for demonstration purposes.
  35.                 7/14/1999    KG                Updated for Metrowerks Codewarror Pro 2.1
  36.                 
  37.  
  38. */
  39. #include "CarbonPrefix.h"
  40. #include <Quickdraw.h>
  41. #include <Palettes.h>
  42. #include <Events.h>
  43. #include <Menus.h>
  44. #include <Windows.h>
  45. #include <Dialogs.h>
  46. #include <Devices.h>
  47. #include <Events.h>
  48. #include <ToolUtils.h>
  49. #include <Sound.h>
  50. #include <Gestalt.h>
  51.     
  52. /* Constants */
  53. #define appleID                1000            /* resource IDs/menu IDs for Apple, */
  54. #define fileID                1001            /*     File and */
  55. #define editID                1002            /*    Edit menus */
  56.     
  57. #define appleM                0                /* Index for each menu in myMenus (array of menu handles) */
  58. #define fileM                1
  59. #define editM                2
  60.  
  61. #define menuCount            3                /* Total number of menus */
  62.  
  63. #define windowID            1000            /* Resource ID for main window */
  64. #define aboutMeDLOG         1000            /* And Resource ID for About box dialog. */
  65.  
  66. #define tubularItem            1                /* When checked, animation of colors. */
  67. #define quitItem            3                /* Quit in the menu of course. */
  68.  
  69. #define aboutMeCommand        1                /* Menu item in apple menu for About TubeTest item */
  70.     
  71. #define totalColors            152                /* use 150 colors in our palette for drawing eyes. */
  72. #define numColors            150                /* to skip black and white. */
  73.     
  74.     
  75. /* Globals */
  76. MenuHandle    myMenus[menuCount];
  77. Rect        dragRect;                        /* Rectangle used to mark bounds for dragging window */
  78. Boolean        doneFlag,                        /* true if user has chosen Quit command */
  79.             tubeCheck;                        /* if true, the menu is checked, and we animate. */
  80. EventRecord    myEvent;
  81. WindowPtr    myWindow,
  82.             whichWindow;
  83. char        theChar;
  84. OSErr        error;
  85. SysEnvRec    theWorld;
  86.  
  87. /* Prototypes */
  88. void DrawEyes();
  89. void SetUpMenus();
  90. void ShiftyColors();
  91. void ShowAboutMeDialog();
  92. void DoCommand(long int mResult);
  93.  
  94.  
  95. /* Make the 2.0 Interface for passing Points to the Toolbox by address,
  96.    emulate the 3.0 method of passing Points by value. This list only contains
  97.    the affected routines actually used in this sample, and is not intended to
  98.    be a comprehensive list of the affected routines. */
  99. #ifdef MPW2
  100. #    define FindWindow    FINDWINDOW
  101. #    define MenuSelect    MENUSELECT
  102. #endif
  103.  
  104.     
  105. void main()
  106. {
  107.     /*
  108.     **    Test the computer to be sure we can do color.  
  109.     **    If not we would crash, which would be bad.  
  110.     **    If we can’t run, just beep and exit.
  111.     */
  112.     /*error = SysEnvirons(1, &theWorld);
  113.     if (theWorld.hasColorQD == false) {
  114.         SysBeep (50);
  115.         ExitToShell();                            // If no color QD, we must leave. 
  116.     };*/
  117.     
  118.     long    result;
  119.     BitMap    bitMap;
  120.     
  121.     error = Gestalt(gestaltQuickdrawVersion, &result);
  122.     if (result < gestalt8BitQD) {
  123.         SysBeep (50);
  124.         ExitToShell();
  125.     }
  126.  
  127.     /*InitGraf(&qd.thePort);
  128.     InitFonts();
  129.     InitWindows();
  130.     InitMenus();
  131.     TEInit();
  132.     InitDialogs(nil);*/
  133.     InitCursor();
  134.  
  135.     GetQDGlobalsScreenBits(&bitMap);
  136.     //SetRect(&dragRect, 4, 24, qd.screenBits.bounds.right - 4, qd.screenBits.bounds.bottom - 4);
  137.     SetRect(&dragRect, 4, 24, bitMap.bounds.right - 4, bitMap.bounds.bottom - 4);
  138.     doneFlag = false;                            /* flag to detect when Quit command is chosen */
  139.     tubeCheck = false;                            /* flag for animating color is initially off. */
  140.  
  141.     /*
  142.     **    Open the color window.
  143.     */
  144.     myWindow = GetNewCWindow(windowID, nil, (WindowPtr) -1);
  145.     //SetPort(myWindow);
  146.     SetPortWindowPort(myWindow);
  147.  
  148.     /*
  149.     **    Set up menus last, since the menu drawing can then use 
  150.     **    the palette we have for our window. 
  151.     **    Makes the Apple look better, in particular.
  152.     */
  153.     SetUpMenus();
  154.     
  155.     /*
  156.     **    Main Event Loop
  157.     */
  158.     do {
  159.         //SystemTask();    //er, what does(did) this do?
  160.  
  161.         if (GetNextEvent(everyEvent, &myEvent)) {
  162.             switch (myEvent.what) {                /* case on event type */
  163.  
  164.                 case mouseDown:
  165.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  166.  
  167.                         case inSysWindow:        /* desk accessory window: call Desk Manager to handle it */
  168.                             //SystemClick(&myEvent, whichWindow);
  169.                             break;
  170.  
  171.                         case inMenuBar:            /* Menu bar: learn which command, then execute it. */
  172.                             DoCommand(MenuSelect(myEvent.where));
  173.                             break;
  174.  
  175.                         case inDrag:            /* title bar: call Window Manager to drag */
  176.                             DragWindow(whichWindow, myEvent.where, &dragRect);
  177.                             break;
  178.  
  179.                         case inContent:            /* body of application window: */
  180.                             if (whichWindow != FrontWindow())
  181.                                 SelectWindow(whichWindow); /* and make it active if not */
  182.                             break;
  183.                     }
  184.                     break;
  185.  
  186.                 case updateEvt:                    /* Update the eyes in window. */
  187.                     if ((WindowPtr) myEvent.message == myWindow) {
  188.                         BeginUpdate((WindowPtr) myEvent.message);
  189.                         DrawEyes();
  190.                         EndUpdate((WindowPtr) myEvent.message);
  191.                     }
  192.                     break;
  193.                             
  194.                 case keyDown:
  195.                 case autoKey:                    /* key pressed once or held down to repeat */
  196.                     if (myWindow == FrontWindow()) {
  197.                         theChar = (myEvent.message & charCodeMask); /* get the char */
  198.                         /* 
  199.                         **    If Command key down, do it as a Menu Command.
  200.                         */
  201.                         if (myEvent.modifiers & cmdKey)
  202.                             DoCommand(MenuKey(theChar));
  203.                     }
  204.                     break;
  205.  
  206.             }
  207.         }
  208.  
  209.         /*    
  210.         **    If we have menu item checked, go ahead and animate colors.
  211.         */
  212.         if (tubeCheck) ShiftyColors();
  213.         
  214.     } while (!doneFlag);
  215.  
  216.     /*
  217.     **    clean up after palette manager, 
  218.     **    so he can chuck the palette in use.
  219.     */
  220.     //DisposeWindow (myWindow);
  221. }
  222.  
  223. /*
  224. **    This routine will update the window when required by update events.    It
  225. **    will draw two circular dudes that are indexed in colors through the colors
  226. **    we are using. 0 and 1 are skipped, since those are white and black in the
  227. **    palette.
  228. */
  229. void DrawEyes()
  230. {
  231.     Rect    tempRect;
  232.     int        i;
  233.     
  234.     SetRect(&tempRect, numColors, numColors, numColors, numColors);
  235.     for (i = 2; i <= totalColors; i++) {
  236.         PmForeColor(i);
  237.         FrameOval (&tempRect);
  238.         InsetRect (&tempRect, -1, -1);
  239.     }
  240.     
  241.     SetRect(&tempRect, numColors*3, numColors, numColors*3, numColors);
  242.     for (i = totalColors; i >= 2; i--) {
  243.         PmForeColor(i);
  244.         FrameOval (&tempRect);
  245.         InsetRect (&tempRect, -1, -1);
  246.     }
  247. }
  248.  
  249.  
  250. /*
  251. **    Read menu descriptions from resource file into memory 
  252. **    and store handles in myMenus array.
  253. **    Insert into MenuBar and draw.
  254. */
  255. void SetUpMenus()
  256. {
  257.     int i;
  258.  
  259.     myMenus[appleM] = GetMenu(appleID);         /* read Apple menu from resource file */
  260.     AppendResMenu(myMenus[appleM], 'DRVR');        /* add desk accessory names to Apple menu */
  261.     myMenus[fileM] = GetMenu(fileID);            /* read file menu from resource file */
  262.     myMenus[editM] = GetMenu(editID);            /* read edit menu from resource file */
  263.  
  264.     for (i = 0; i < menuCount; i++) 
  265.         InsertMenu(myMenus[i], 0);                 /* install menus in menu bar */
  266.     
  267.     DrawMenuBar();                                /* and draw menu bar */
  268. }
  269.  
  270.  
  271. /*
  272. **    Use the Palette currently attached to the main window to animate the colors 
  273. **    in the circular eye shapes.  This will rotate them around to give the flowing 
  274. **    tube effect. We make the palette into a color table so we can move entries 
  275. **    around.    We have to skip the first two entries since those are black and white. 
  276. **    (entries 0 and 1)
  277. */
  278. void ShiftyColors()
  279. {
  280.     
  281.     PaletteHandle    currPalette;
  282.     CTabHandle        destCTab;
  283.     ColorSpec        lastCSpec;
  284.     
  285.     //SetPort (myWindow);
  286.     SetPortWindowPort(myWindow);
  287.     
  288.     currPalette = GetPalette(myWindow);
  289.     destCTab = (CTabHandle) NewHandle(sizeof(ColorTable)+(totalColors*sizeof(ColorSpec)));
  290.     if (destCTab == nil)  return;
  291.     Palette2CTab(currPalette, destCTab);
  292.     
  293.     /*
  294.     **    Move the colors around in the color table, skipping 0 and 1, and moving
  295.     **    all the elements down by one, and copying the element at 2 back to the 
  296.     **    end of the table. The effect is to rotate the colors in the table.
  297.     */
  298.     lastCSpec = (*destCTab)->ctTable[2];                        /* pull first one off. */
  299.     BlockMove (&(*destCTab)->ctTable[3], 
  300.                &(*destCTab)->ctTable[2], 
  301.                (numColors) * sizeof(ColorSpec) );                /* copy all one entry down. */
  302.     (*destCTab)->ctTable[totalColors-1] = lastCSpec;            /* put last color back on front. */
  303.         
  304.     AnimatePalette(myWindow, destCTab, 2, 2, numColors);
  305.     
  306.     DisposeHandle ((Handle) destCTab);
  307. }
  308.  
  309.  
  310. /*    
  311. **    Display the dialog box in response to the 'About TubeTest' menu item
  312. */
  313. void ShowAboutMeDialog()
  314. {
  315.     DialogPtr    theDialog;
  316.     short        itemHit;
  317.  
  318.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
  319.     ModalDialog(nil, &itemHit);
  320.     DisposeDialog(theDialog);
  321. }
  322.  
  323.  
  324. /*
  325. **    Execute menu command specified by mResult,
  326. **    the result of MenuSelect
  327. */
  328. void DoCommand(long int mResult)
  329. {
  330.     short    theItem,                            /* menu item number from mResult low-order word */
  331.             theMenu;                            /* menu number from mResult high-order word */
  332.     //Str255    name;                                /* desk accessory name */
  333.     //int        temp;
  334.     //Boolean    dummy;
  335.  
  336.     theItem = LoWord(mResult);                    /* call Toolbox Utility routines to */
  337.     theMenu = HiWord(mResult);                    /* set menu item number and menu */
  338.  
  339.     switch (theMenu) {                            /* switch on menu ID */
  340.  
  341.         case appleID:
  342.             if (theItem == aboutMeCommand)
  343.                 ShowAboutMeDialog();
  344.             else {
  345.                     /*GetMenuItemText(myMenus[appleM], theItem, name);
  346.                     temp = OpenDeskAcc(name);*/
  347.                     //SetPort(myWindow);
  348.                     SetPortWindowPort(myWindow);
  349.             }
  350.             break;
  351.  
  352.         case fileID:
  353.             if (theItem == quitItem)
  354.                 doneFlag = true;
  355.             else if (theItem == tubularItem) {
  356.                     tubeCheck = !tubeCheck;
  357.                     //CheckItem(myMenus[fileM], tubularItem, tubeCheck);
  358.                     CheckMenuItem(myMenus[fileM], tubularItem, tubeCheck);
  359.             }
  360.             break;
  361.  
  362.         case editID:
  363.             //dummy = SystemEdit(theItem - 1);    /* Pass the command on to the Desk Manager. */
  364.             break;
  365.     }
  366.  
  367.     HiliteMenu(0);                                /* Unhighlight menu title */
  368.                                                 /* (highlighted by MenuSelect) */
  369. }
  370.